package com.achievo.know.job;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.commons.io.FileUtils;

import com.achievo.know.application.FileApplication;
import com.achievo.know.component.impl.FileManagerConfig;
import com.achievo.know.domain.FileAttachment;
import com.achievo.know.pojo.Constants;

public class ConvertFileThread implements Callable<Boolean> {
	
	private FileManagerConfig config;
	private String monthPath;
	private String guid;
	private String ext;
	private FileApplication fileApplication;
	
	public ConvertFileThread(FileManagerConfig config, String monthPath,
			String guid, String ext,FileApplication fileApplication) {
		super();
		this.config = config;
		this.monthPath = monthPath;
		this.guid = guid;
		this.ext = ext;
		this.fileApplication=fileApplication;
	}

	@Override
	public Boolean call() throws Exception {
		boolean isOk =false;
		String sourcePath = config.getPersistFileStoreDirectory()
		+ java.io.File.separator + monthPath
		+ guid + "." + ext;
		String destDesk = config.getSwfFileStoreDirectory()
		+ java.io.File.separator + guid;
		java.io.File desk=new java.io.File(destDesk);
		if(!desk.exists()){
			desk.mkdir();
		}
		FileAttachment atts = fileApplication.getAttachmentByGuid(guid);
		if (atts != null) {
			atts=fileApplication.getAttachment(atts.getId());
			atts.setConvertError("0");//正在转换
			fileApplication.saveOrUpdateFileAttachment(atts);
			
			
			
			/*int size=atts.getAttachSize()/1024;
			System.out.println(size);
			if(size<10){//10m之 内
				String destPath = destDesk + java.io.File.separator+ "%.swf";
				
				final String cmd = config.getConfigHolder().getConfig(
						Constants.PDF2SWF)
						+ " " + sourcePath + " -o " + destPath + " -T 9 -f -S -s languagedir="+config.getConfigHolder().getConfig(
								Constants.XPDF);
				Process pro = Runtime.getRuntime().exec(cmd);
				BufferedReader bufferedReader = new BufferedReader(
						new InputStreamReader(pro.getInputStream()));
				while (bufferedReader.readLine() != null);
				try {
					pro.waitFor();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				java.io.File oFile=new java.io.File(config.getSwfFileStoreDirectory()+java.io.File.separator+guid+ java.io.File.separator+"1.swf");
				if(oFile.exists()){
					isOk= true;
				}
				// update db record
			
				atts = fileApplication.getAttachmentByGuid(guid);
				atts.setConvertOk(isOk);
				if (!isOk) {
					atts.setConvertError("1");
				}
				fileApplication.saveOrUpdateFileAttachment(atts);
			}else{*/
				String destPath = destDesk + java.io.File.separator+ "%%.swf";
				
				final String cmd = config.getConfigHolder().getConfig(
						Constants.PDF2SWF)
						+ " " + sourcePath + " -o " + destPath + " -T 9 -f -S -s languagedir="+config.getConfigHolder().getConfig(
								Constants.XPDF);
				java.io.File batinFile=new java.io.File(config.getConfigHolder().getConfig(
						Constants.FOLDER_BAT)+File.separator+"in"+File.separator+guid+".bat");
				List<String> cmds=new ArrayList<String>();
				cmds.add(cmd);
				cmds.add("move %0 ../ok");
				FileUtils.writeLines(batinFile, cmds);
				java.io.File batokFile=new java.io.File(config.getConfigHolder().getConfig(
						Constants.FOLDER_BAT)+File.separator+"ok"+File.separator+guid+".bat");
				while(!batokFile.exists()){//只要存在就继续检查
					Thread.sleep(3000);
				}
				java.io.File oFile=new java.io.File(config.getSwfFileStoreDirectory()+java.io.File.separator+guid+ java.io.File.separator+"1.swf");
				if(oFile.exists()){
					isOk= true;
				}
				if(isOk){
					atts=fileApplication.getAttachment(atts.getId());
					atts.setConvertOk(true);
					atts.setConvertError("2");
					fileApplication.saveOrUpdateFileAttachment(atts);
				}else{
					atts=fileApplication.getAttachment(atts.getId());
					atts.setConvertError("1");
					atts.setConvertOk(false);
					fileApplication.saveOrUpdateFileAttachment(atts);
				}
			//}
		}
		System.out.println("converting file " + guid + " " + isOk);
		return isOk;
	}

}
